<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :sm="24" :md="24" :xxl="16">
            <a-form-item label="区域">
              <a-row :gutter="5" type="flex">
                <a-col :span="4">
                  <a-select
                    placeholder="省"
                    v-model="queryParam.provinceCode"
                    @change="e => addressChange(e, 'cityOptions', 'province')"
                  >
                    <a-select-option v-for="item in provinceOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="市"
                    v-model="queryParam.cityCode"
                    @change="e => addressChange(e, 'areaOptions', 'city')"
                  >
                    <a-select-option v-for="item in cityOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="区"
                    v-model="queryParam.areaCode"
                    @change="e => addressChange(e, 'streetOptions', 'area')"
                  >
                    <a-select-option v-for="item in areaOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="街道"
                    v-model="queryParam.streetCode"
                    @change="e => addressChange(e, '', 'street')"
                  >
                    <a-select-option v-for="item in streetOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="商家"
                    v-model="queryParam.businessId"
                    @change="e => agentChange(e, 'networkOptions', 'agentCode')"
                  >
                    <a-select-option v-for="item in agentOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="网点"
                    v-model="queryParam.networkId"
                    @change="e => agentChange(e, 'deviceOptions', 'networkCode')"
                  >
                    <a-select-option v-for="item in networkOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
              </a-row>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="创建时间">
              <a-range-picker allowClear class="per100" v-model="tempTimes" @change="timeChange" />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="用户">
                <a-input placeholder="请输入" v-model="queryParam.wechatMobile"></a-input>
              </a-form-item>
            </a-col>
          </template>

          <a-col :sm="24" :md="12" :xl="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域 -->
    <a-table
      ref="table"
      bordered
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :scroll="{ x: 1200 }"
      @change="handleTableChange"
    >
      <span slot="status" slot-scope="text, record">
        <a-tag color="green" v-if="text === 1">
          {{ filterDictText(approvalStatusOptions, text) }}
        </a-tag>
        <a-tag color="red" v-else-if="text === 2">
          {{ filterDictText(approvalStatusOptions, text) }}
        </a-tag>
        <a-tag color="blue" v-else>
          {{ filterDictText(approvalStatusOptions, text) }}
        </a-tag>
      </span>
      <span slot="type" slot-scope="text, record">
        <a-tag color="#04de6d" v-if="text === 1">
          {{ filterDictText(withdrawTypeOptions, text) }}
        </a-tag>
        <a-tag color="#000" v-else-if="text === 2">
          {{ filterDictText(withdrawTypeOptions, text) }}
        </a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <div v-if="filterDictText(approvalStatusOptions, record.status) === '待审批'">
          <a-button type="primary" class="ml5 mr5" size="small" @click="approveWithdrawal(record)">通过</a-button>
          <a-button
            type="orange"
            class="ml5 mr5"
            size="small"
            @click="
              data.id = record.id
              data.refuseReason = ''
              visible = true
            "
            >拒绝</a-button
          >
        </div>

        <a v-else-if="filterDictText(approvalStatusOptions, record.status) === '通过'" :style="{ color: '#52c41a' }"
          >已通过</a
        >
        <a v-else :style="{ color: '#f5222d' }">已拒绝</a>
      </span>
    </a-table>

    <a-modal v-model="visible" title="拒绝提现" @ok="rejectWithdrawal">
      <a-textarea v-model="data.refuseReason" placeholder="请输入拒绝提现原因" />
    </a-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterDictText } from '@/components/dict/JDictSelectUtil'
import Api from '@/api/MemberManage/MemberWithdrawal'
import FundsWithdrawal from '@api/CashManage/FundsWithdrawal'
import { putAction } from '@api/manage'
import { formatAmountFromFenToYuan } from '@/utils/util'

export default {
  name: 'MemberWithdrawal',
  mixins: [JeecgListMixin],
  data() {
    return {
      filterDictText,
      url: {
        list: Api.list,
        edit: FundsWithdrawal.edit
      },
      columns: [],
      withdrawTypeOptions: [],
      approvalStatusOptions: [],
      initDictOptions,
      visible: false,
      data: {
        id: '',
        status: 2,
        refuseReason: ''
      }
    }
  },
  created() {
    this.columns = [
      {
        title: '用户ID',
        align: 'center',
        dataIndex: 'userId',
        sorter: (a, b) => {
          return a.userId - b.userId
        }
      },
      {
        title: '用户',
        align: 'center',
        dataIndex: 'wechatMobile'
      },
      {
        title: '微信昵称',
        align: 'center',
        dataIndex: 'wechatName'
      },
      {
        title: '提现金额（元）',
        dataIndex: 'amount',
        align: 'center',
        customRender: (text, record, index) => {
          return formatAmountFromFenToYuan(text)
        },
        sorter: (a, b) => {
          return a.amount - b.amount
        }
      },
      {
        title: '提现方式',
        dataIndex: 'type',
        align: 'center',
        scopedSlots: { customRender: 'type' },
        sorter: (a, b) => {
          return a.type - b.type
        }
      },
      {
        title: '审批状态',
        dataIndex: 'status',
        align: 'center',
        scopedSlots: { customRender: 'status' },
        sorter: (a, b) => {
          return a.status - b.status
        }
      },
      {
        title: '审批人',
        align: 'center',
        dataIndex: 'approver',
        sorter: (a, b) => {
          return a.approver - b.approver
        }
      },
      {
        title: '审批时间',
        align: 'center',
        dataIndex: 'approveTime',
        sorter: (a, b) => {
          return a.approveTime - b.approveTime
        }
      },
      {
        title: '拒绝原因',
        align: 'center',
        dataIndex: 'refuseReason',
        sorter: (a, b) => {
          return a.refuseReason - b.refuseReason
        }
      },
      {
        title: '创建时间',
        align: 'center',
        dataIndex: 'createTime',
        sorter: (a, b) => {
          return a.createTime - b.createTime
        }
      },
      {
        title: '操作',
        dataIndex: 'action',
        fixed: 'right',
        width: 200,
        scopedSlots: { customRender: 'action' }
      }
    ]

    this.getAddress('provinceOptions')
  },
  methods: {
    // 初始化数据字典
    initDictConfig() {
      //初始化字典 - 提现方式
      initDictOptions('withdrawType').then(res => {
        if (res.success) {
          this.withdrawTypeOptions = res.result
        }
      })
      //初始化字典 - 审批状态
      initDictOptions('approvalStatus').then(res => {
        if (res.success) {
          this.approvalStatusOptions = res.result
        }
      })
    },
    // 创建时间改变
    timeChange(e, arr) {
      if (arr[0]) {
        this.queryParam.startTime = arr[0]
        this.queryParam.endTime = arr[1]
      } else {
        this.queryParam.startTime = undefined
        this.queryParam.endTime = undefined
      }
    },

    approveWithdrawal(record) {
      putAction(this.url.edit, { id: record.id, status: 1 }).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },

    rejectWithdrawal() {
      putAction(this.url.edit, this.data)
        .then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.visible = false
        })
    }
  }
}
</script>

<style scoped></style>

